<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>参数列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-alert show-icon title="注意:只可以选择三级分类商品"  type="warning" :closable="false">
            </el-alert>
            <!-- 商品分类区 -->
            <el-row>
                <el-col :span='5'>
                    <span>商品分类:</span><el-cascader expandTrigger='hover' v-model="cateListId" :options="cateList" :props="props" @change="handleChange">
                    </el-cascader>    
                </el-col>
            </el-row>
            <!-- 静态动态属性 -->
        <el-tabs v-model="activeName" @tab-click="handleTabsClick">
            <el-tab-pane label="动态参数" name="many">
                <!-- 添加动态参数按钮 -->
                <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">添加参数</el-button>
                <el-table :data="manyData" style="width: 100%" border>
                    <el-table-column type="expand">
                        <template slot-scope="scope">
                           <el-tag
                            v-for="(item,i) in scope.row.attr_vals"
                            :key="i"
                            closable @close='closeTags(i,scope.row)'>
                            {{item}}
                            </el-tag>
                            <el-input
                            class="input-new-tag"
                            v-if="scope.row.inputVisible"
                            v-model="scope.row.inputValue"
                            ref="saveTagInputRef"
                            @keyup.enter.native="handleInputConfirm(scope.row)"
                            @blur="handleInputConfirm(scope.row)"
                            >
                            </el-input>
                            <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column type="index">
                    </el-table-column>
                    <el-table-column prop="attr_name" label="分类名称">
                    </el-table-column>
                    <el-table-column  label="操作">
                        <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editParams(scope.row.attr_id)">编辑</el-button>
                        <el-button type="warning" icon="el-icon-delete" size="mini"  @click="deleteParams(scope.row.attr_id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="静态属性" name="only">
                <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">添加属性</el-button>
                <!-- 静态属性面板 -->
                 <el-table :data="onlyData" style="width: 100%" border>
                    <el-table-column type="expand">
                        <template slot-scope="scope">
                           <el-tag
                            v-for="(item,i) in scope.row.attr_vals"
                            :key="i"
                            closable @close='closeTags(i,scope.row)'>
                            {{item}}
                            </el-tag>
                            <el-input
                            class="input-new-tag"
                            v-if="scope.row.inputVisible"
                            v-model="scope.row.inputValue"
                            ref="saveTagInputRef"
                            @keyup.enter.native="handleInputConfirm(scope.row)"
                            @blur="handleInputConfirm(scope.row)"
                            >
                            </el-input>
                            <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column type="index">
                    </el-table-column>
                    <el-table-column prop="attr_name" label="分类名称">
                    </el-table-column>
                    <el-table-column  label="操作">
                      <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editParams(scope.row.attr_id)">编辑</el-button>
                        <el-button type="warning" icon="el-icon-delete" size="mini" @click="deleteParams(scope.row.attr_id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
        </el-tabs>
        </el-card>
        <!-- 添加属性对话框 -->
        <el-dialog
            :title="'添加'+textL"
            :visible.sync="addDialogVisible"
            width="50%"
            @close='addDialogClosed'>
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" >
                <el-form-item :label="textL" prop="name">
                <el-input v-model="addForm.attr_name"></el-input>
            </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑对话框 -->
        <el-dialog
            :title="'修改'+textL"
            :visible.sync="editDialogVisible"
            width="50%"
            @close='editDialogClosed'>
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" >
                <el-form-item :label="textL" prop="name">
                <el-input v-model="editForm.attr_name"></el-input>
            </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editCate">确 定</el-button>
            </span>
        </el-dialog>
    </div>  
</template>

<script>
export default {
    data(){
        return{
            cateList:[],
            cateListId:[],
            props:
                {
                    value:'cat_id',
                    label:'cat_name',
                    children:'children'
                },
            activeName:'many',
            manyData:[],
            onlyData:[],
            addDialogVisible:false,
            addForm:{},
            addFormRules:{
                name: [
                { required: false, message: '请输入活动名称', trigger: 'blur' },
                { min: 1, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                     ]
            },
            editDialogVisible:false,
            editForm:{},
            editFormRules:{
                name: [
                { required: false, message: '请输入活动名称', trigger: 'blur' },
                { min: 1, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                     ]
            }    
        }
    },
    created(){
        this.getCateList()
    },
    methods:{
        async getCateList(){

          const {data:res} = await this.$http.get('categories')
          this.cateList = res.data
        //   console.log(this.cateList);
          

        },
        handleChange(){
            this.getParamsData()
        },
        handleTabsClick(){
            this.getParamsData()
            console.log(this.activeName)
        },
        async getParamsData(){
            if(this.cateListId.length!==3){
                this.cateListId=[]
                this.manyData=[]
                this.onlyData = []
                return 
            }
            const {data:res}= await this.$http.get(`categories/${this.cateId}/attributes`,{params:{sel:this.activeName}})
            console.log(res.data)
            // res.data.attr_vals = res.data.attr_vals.split(' ')
        
            res.data.forEach(item=>{
                if(item.attr_vals===''){
                    item.attr_vals=[]
                }else{
                     item.attr_vals = item.attr_vals.split(' ')
                     
                }
                item.inputVisible=false
                     item.inputValue=''
               
            })
            // console.log(expandTag)
            if(this.activeName==='many'){
                this.manyData = res.data
            }else{
                this.onlyData = res.data
            }
            
        },
        addDialogClosed(){
            this.$refs.addFormRef.resetFields()
        },
        addCate(){
            this.$refs.addFormRef.validate(async valid=>{
                if(!valid){
                    return
                }else{
                  const {data:res}= await this.$http.post(`categories/${this.cateId}/attributes`,
                    {
                        attr_name:this.addForm.attr_name,
                        attr_sel:this.activeName
                    })
                }
                // this.cateList =res.data
                this.getParamsData()
                
                this.addDialogVisible = false
                
                
            })
        },
        editDialogClosed(){
            this.$refs.editFormRef.resetFields()
        },
        async editParams(attrId){
          const {data:res} = await this.$http.get(`categories/${this.cateId}/attributes/${attrId}`,{params:{attr_sel:this.activeName}})
          this.editForm = res.data
          this.editDialogVisible=true
        },
        async editCate(){
           
            const {data:res1}= await this.$http.put(`categories/${this.cateId}/attributes/${this.editForm.attr_id}`,
                {
                attr_name:this.editForm.attr_name,
                attr_sel:this.activeName
                }
            )
              
            console.log(res1)
            // if(res.meta.status==200){
                  this.getParamsData()
                 this.editDialogVisible=false
            // }

            
            
        },
        async deleteParams(attrId){
            const resultF = await this.$confirm('此操作将永久删除该属性, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err=>err)
            if(resultF!=='confirm'){
                return this.$message.info('取消删除')
            }
            const {data:res}=await this.$http.delete(`categories/${this.cateId}/attributes/${attrId}`)
            if(res.meta.status===200){
                this.getParamsData()
            }
            
        },
        async handleInputConfirm(row){
            if(row.inputValue.trim().length===0){
                row.inputValue=''    
                row.inputVisible = false
                return 
            }
            row.attr_vals.push(row.inputValue)
            row.inputValue=''
            row.inputVisible = false
            this.saveAttribute(row)
            
        },
        async saveAttribute(row){
            const {data:res}=await this.$http.put(`categories/${this.cateId}/attributes/${row.attr_id}`,
                    {
                    attr_name:row.attr_name,
                    attr_sel:row.attr_sel,
                    attr_vals:row.attr_vals.join(' ')
                    }
                )
                console.log(res)
            if(res.meta.status===200){
                this.$message.success("修改成功")
                // row.attr_vals.push(res.data.attr_vals)
            }
        },
        closeTags(i,row){
            row.attr_vals.splice(i,i)
            this.saveAttribute(row)
        },
        showInput(row){
            row.inputVisible = true
           this.$nextTick(_ => {
          this.$refs.saveTagInputRef.$refs.input.focus();
        });
        }
    },
    computed:{
        isBtnDisabled(){
            if(this.cateListId.length===3){
                return false
            }
            return true
        },
        cateId(){
            if(this.cateListId.length===3)
            return this.cateListId[2]
        },
        textL(){
            if(this.activeName==='many'){
                return "动态参数"
            }else{
                return "静态属性"
            }
        }
    }
}
</script>

<style scope>
.input-new-tag{
    width: 150px !important;
}
.el-tag{
    margin: 10px;
}

</style>